<?php

namespace app\common\business\http\wechat;

/**
 *   http数据来源抽象类
 */
class AbstractModel extends \app\common\business\http\AbstractModel {

    /**
     * 访问的host
     * 
     * @var string
     */
    protected $_host = "";

    /**
     * 授权token
     * 
     * @var string 
     */
    protected $_accessToken = "";

    /**
     * 构造函数，实现初始化host
     */
    public function __construct() {
        $this->_host = config("api_url");
    }

    /**
     * 发起HTTP请求
     * 
     * @param string $url
     * @param string $method
     * @param array $params
     * @param int $timeout
     * @return boolean
     */
    protected function _request($url, $method = "GET", $params = array(), $timeout = 30, $checkErrno = true) {
        $result = parent::_request($url, $method, $params, $timeout);
        $data = json_decode($result, true);
        if (json_last_error() != JSON_ERROR_NONE) {
            exception("返回数据不是json格式，url:" . $url, 100011);
        }

        if ($data['errno'] == 200) {
             return isset($data['data']) ? $data['data'] : "";
        }
        exception($data['errmsg'], 100011);
    }

}
